package scfw.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import scfw.dao.SysRoleMapper;
import scfw.dao.SysUserMapper;
import scfw.model.SysRole;
import scfw.model.SysUser;
import scfw.model.comm.CT;
import scfw.service.model.LoginInfoVo;
import scfw.service.model.UserSrModel;

import java.util.*;

@Service("userService")
public class UserServiceImpl implements UserServiceI {

	@Autowired
	private SysUserMapper sysUserMapper;

	@Autowired
	private SysRoleMapper roleMapper;

	@Autowired
	private ValSetServiceI valSetService;

	public SysUser getUserById(int id)
	{
		return this.sysUserMapper.selectByPrimaryKey(Integer.valueOf(id));
	}

	public List<SysUser> querySysUser()
	{
		return this.sysUserMapper.querySysUser();
	}

	public UserSrModel addUser()
	{
		UserSrModel srModel = new UserSrModel();

		Map roleMap = new LinkedHashMap();
		List<SysRole> roleList = this.roleMapper.queryRoles();
		for (SysRole item : roleList) {
			roleMap.put(item.getId(), item.getRoleName());
		}
		srModel.setRoleList(roleMap);

		Map ballMap = new HashMap();

		ballMap = this.valSetService.queryValSetMap("ball");
		srModel.setBallMap(ballMap);

		List svss = this.valSetService.queryValSetList("ball");
		srModel.setSvss(svss);

		Map areaList = new HashMap();
		areaList = this.valSetService.queryValSetMap("province");
		srModel.setAreaList(areaList);

		srModel.setUserName("zhangsan");
		srModel.setNickName("张三");
		srModel.setSex(Integer.valueOf(1));
		srModel.setMyBanlance(Double.valueOf(0.0D));
		srModel.setFavoriteBall(Integer.valueOf(1));
		srModel.setWorkarea("1");
		List roles = new ArrayList();
		roles.add(Integer.valueOf(1));
		srModel.setRoles(roles);

		return srModel;
	}

	public UserSrModel editUser(int id)
	{
		UserSrModel srModel = new UserSrModel();

		Map roleMap = new LinkedHashMap();
		List<SysRole> roleList = this.roleMapper.queryRoles();
		for (SysRole item : roleList) {
			roleMap.put(item.getId(), item.getRoleName());
		}
		srModel.setRoleList(roleMap);

		Map ballMap = new HashMap();

		ballMap = this.valSetService.queryValSetMap("ball");
		srModel.setBallMap(ballMap);

		List svss = this.valSetService.queryValSetList("ball");
		srModel.setSvss(svss);

		Map areaList = new HashMap();
		areaList = this.valSetService.queryValSetMap("province");
		srModel.setAreaList(areaList);

		SysUser su = this.sysUserMapper.selectByPrimaryKey(Integer.valueOf(id));
		BeanUtils.copyProperties(su, srModel);

		List roles = new ArrayList();
		String powers = su.getPower().substring(1, su.getPower().length() - 1);
		if (powers.contains("|")) {
			String[] power = StringUtils.split(powers, "|");
			for (String p : power)
				if (!StringUtils.isEmpty(p))
					roles.add(Integer.valueOf(Integer.parseInt(p)));
		}
		else {
			roles.add(Integer.valueOf(Integer.parseInt(powers)));
		}
		srModel.setRoles(roles);

		return srModel;
	}

	public int submitUser(UserSrModel srModel)
	{
		SysUser record = new SysUser();
		BeanUtils.copyProperties(srModel, record);

		record.setIsDisable("0");

		String power = "|";
		for (Integer role : srModel.getRoles()) {
			power = power + role + "|";
		}
		record.setPower(power);
		record.setTrueName(srModel.getNickName());
		if (0 >= srModel.getId()) {
			if ((null == record.getUserPwd()) || ("".equals(record.getUserPwd()))) record.setUserPwd("1234");
			record.setPwdType("1");
			record.setCreateDate(new Date());
			this.sysUserMapper.insert(record);
		} else {
			Date d = CT.CURRENT_DATE;
			record.setUpdateDate(new Date());
			this.sysUserMapper.updateByPrimaryKeySelective(record);
		}

		int id = record.getId().intValue();
		return id;
	}

	public int deleteUserById(int id)
	{
		return this.sysUserMapper.deleteByPrimaryKey(Integer.valueOf(id));
	}

	public UserSrModel loginUser(UserSrModel loginForm)
	{
		SysUser su = this.sysUserMapper.getUserByName(loginForm.getUserName());
		if ((su != null) && (su.getUserPwd().equals(loginForm.getUserPwd()))) {
			BeanUtils.copyProperties(su, loginForm);
			return loginForm;
		}
		return null;
	}

	public UserSrModel registUser(UserSrModel registForm)
			throws Exception
	{
		SysUser su = this.sysUserMapper.getUserByName(registForm.getUserName());
		if (su != null) {
			throw new Exception("您注册的用户名已经存在！");
		}
		SysUser user = new SysUser();
		user.setUserName(registForm.getUserName());
		user.setUserPwd(registForm.getUserPwd());
		user.setNickName(registForm.getNickName());

		user.setCreateDate(CT.CURT_DATE());
		user.setFavoriteBall(Integer.valueOf(1));
		user.setIsDisable("0");
		user.setMyBanlance(Double.valueOf(18.0D));
		user.setPower("|2|");
		user.setPwdType("1");
		user.setSex(Integer.valueOf(1));
		user.setTrueName(registForm.getNickName());
		user.setWorkarea("1");
		this.sysUserMapper.insert(user);

		return registForm;
	}

	@Override
	public UserSrModel ssoLogin(LoginInfoVo loginInfoVo) {
		UserSrModel loginForm = new UserSrModel();
		SysUser su = this.sysUserMapper.getUserByName(loginInfoVo.getUsername());
		if (su != null) {
			BeanUtils.copyProperties(su, loginForm);
			return loginForm;
		}
		return null;
	}
}